package com.example.fengyungo.dao;

import com.example.fengyungo.domain.pojo.Driver;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DriverDao {
    /**
     * 通过用户名和密码查找司机
     *
     * @return List<Driver>
     */
    @Select("select * from driver where username = #{username} and password = #{password}")
    List<Driver> getDriverByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    /**
     * 新增司机用户
     *
     * @param driver Driver
     * @return Boolean
     */
    @Insert("insert into driver (id, username, sex, phone, password, idCard, avatar, status, createTime, nickname, mail) VALUES " +
            "(#{id},#{username},#{sex},#{phone},#{password},#{idCard},#{avatar},#{status},#{createTime},#{nickname},#{mail})")
    Boolean addDriver(Driver driver);

    /**
     * 通过用户名（账号）查找司机
     *
     * @return List<Driver>
     */
    @Select("select * from driver where username = #{username}")
    Driver getDriverByUsername(@Param("username") String username);

    /**
     * 通过邮箱查找司机
     *
     * @return List<Driver>
     */
    @Select("select * from driver where mail = #{mail}")
    List<Driver> getDriverByMail(@Param("mail") String mail);

    /**
     * 修改司机昵称
     *
     * @param nickname 昵称
     * @param id       司机id
     * @return Integer
     */
    @Update("update driver set nickname = #{nickname} where id = #{id}")
    Integer updateNicknameById(@Param("nickname") String nickname, @Param("id") String id);

    /**
     * 修改司机性别
     *
     * @param sex 性别
     * @param id  司机id
     * @return Integer
     */
    @Update("update driver set sex = #{sex} where id = #{id}")
    Integer updateSexById(@Param("sex") String sex, @Param("id") String id);

    /**
     * 根据id查询对应司机的状态（是否具备接单权利）
     *
     * @param id 司机id
     * @return Integer
     */
    @Select("select status from driver where id = #{id}")
    Integer getStatusById(String id);

    /**
     * 修改司机电话
     *
     * @param phone 电话
     * @param id    司机id
     * @return Integer
     */
    @Update("update driver set phone = #{phone} where id = #{id}")
    Integer updatePhoneById(@Param("phone") String phone, @Param("id") String id);

    /**
     * 查询所有司机信息
     *
     * @return List<Driver>
     */
    @Select("select * from driver")
    List<Driver> getAllDrivers();

    /**
     * 修改司机信息
     *
     * @param driver 司机
     * @return 更新结果数
     */
    Integer updateDriverById(Driver driver);

    /**
     * 根据id查询司机信息
     *
     * @param id 司机id
     * @return Driver
     */
    @Select("select * from driver where id = #{id}")
    Driver getDriverById(String id);

    /**
     * 根据id查询司机电话
     *
     * @param id 司机id
     * @return String
     */
    @Select("select phone from driver where id = #{id}")
    String getPhoneById(String id);

    /**
     * 查询愿意接收订单的司机
     *
     * @param number String订单编号
     * @return List<Driver>
     */
    List<Driver> getDriversByOrderNumber(String number);

    /**
     * 通过用户名查询密码
     *
     * @param username 用户名
     * @return String
     */
    @Select("select password from driver where username = #{username}")
    String getPasswordByUsername(String username);

    /**
     * 修改密码
     *
     * @param username 用户名
     * @param password 新的密码
     * @return Integer
     */
    @Update("update driver set password = #{password} where username = #{username}")
    Integer updatePassword(@Param("username") String username, @Param("password") String password);

    /**
     * 修改司机评分为新的平均分
     *
     * @param id 司机id
     * @return Integer
     */
    @Update("UPDATE driver SET credit =\n" +
            "(\n" +
            "SELECT AVG(credit) FROM tb_order WHERE receiverId = #{id}\n" +
            ")\n" +
            "WHERE id = #{id};")
    Integer updateCredit(String id);

    /**
     * 条件查询司机
     *
     * @param driver Driver
     * @return List<Driver>
     */
    List<Driver> getDriverByCondition(Driver driver);
}
